import request from "../../utils/request";

Page({
    data: {
        videoGroupList: [],
        videoList: [],
        currentIndex: 0,
        currentViewId: 'id-0',
        videoId: 0,
        videoUpdateTime: [], //视频播放的时间
        isTriggered: false
    },

    onLoad: function (options) {
        this.getVideoGroupList();
    },

    async getVideoGroupList() {
        const res = await request('/video/group/list');
        const videoGroupList = res.data.slice(0, 14);
        this.setData({videoGroupList, currentViewId: `id-${videoGroupList[0].id}`});

        //获取第一个nav对应的视频列表
        this.getVideoList(videoGroupList[0].id);
    },

    handleNavTap(e) {
        const {index} = e.currentTarget.dataset;
        const {videoGroupList} = this.data;

        this.setData({currentIndex: index, currentViewId: `id-${videoGroupList[index].id}`});
        //获取对应的视频列表
        this.getVideoList(videoGroupList[index].id);
    },

    async getVideoList(groupId) {
        const res = await request('/video/group', {id: groupId, showLoading: true});
        let videoList = res.datas;
        videoList = videoList.map((item, index) => ({...item, id: index}));
        this.setData({videoList, videoId: videoList[0].data.vid, isTriggered: false});

        //关闭下拉刷新窗口，如果没有调用下拉刷新窗口，直接关闭也没有影响。
        wx.stopPullDownRefresh();
    },

    handlePlay(e) {
        let vid = e.currentTarget.id;
        // 关闭上一个播放的视频 (videoContext.play时会自动暂停前面播放的视频,故下面这几行代码可以省略)
        // if (this.videoContext && this.vid !== vid) {
        //     this.videoContext.stop()
        // }
        this.vid = vid;

        // 更新data中videoId的状态数据
        this.setData({
            videoId: vid
        });
        // 创建控制video标签的实例对象
        this.videoContext = wx.createVideoContext(vid);
        // 判断当前的视频之前是否播放过，是否有播放记录, 如果有，跳转至指定的播放位置
        let {videoUpdateTime} = this.data;
        let videoItem = videoUpdateTime.find(item => item.vid === vid);
        if (videoItem) {
            this.videoContext.seek(videoItem.currentTime);
        }
        this.videoContext.play();
    },

    handleTimeUpdate(e) {
        const {id: vid} = e.currentTarget;
        const {currentTime} = e.detail;
        const {videoUpdateTime} = this.data;
        const existObj = videoUpdateTime.find(item => item.vid === vid);
        if (existObj) {
            existObj.currentTime = currentTime;
        } else {
            videoUpdateTime.push({vid, currentTime});
        }
        this.setData({videoUpdateTime});
    }
    ,
    handleEnded(e) {
        const {id: vid} = e.currentTarget;
        const {videoUpdateTime} = this.data;
        const index = videoUpdateTime.findIndex(item => item.vid === vid);
        videoUpdateTime.splice(index, 1);
        this.setData({videoUpdateTime});
    },

    //自定义下拉刷新的回调
    handleRefresher() {
        this.setData({isTriggered: true}); //表示下拉刷新已触发
        const {videoGroupList, currentIndex} = this.data;
        this.getVideoList(videoGroupList[currentIndex].id);
    },

    //自定义上啦触底的回调，这里面适合做分页的操作，但是网易云没有提供分页接口，所以这里不演示。
    handleToLower() {
        const {videoGroupList, currentIndex} = this.data;
        this.getVideoList(videoGroupList[currentIndex].id);
    },

    //页面的下拉刷新事件(需要在json文件中配置)
    onPullDownRefresh() {
        const {videoGroupList, currentIndex} = this.data;
        this.getVideoList(videoGroupList[currentIndex].id);
    },

    //页面的上拉触底事件(需要在json文件中配置)
    onReachBottom() {
        const {videoGroupList, currentIndex} = this.data;
        this.getVideoList(videoGroupList[currentIndex].id);
    },

    //监听用户点击页面内转发按钮或者右上角菜单转发按钮的行为
    onShareAppMessage(data) {
        const {from, target, webViewUrl} = data;
        console.log(`from: ${from}`);
        console.log(`target: ${target}`);
        console.log(`webViewUrl: ${webViewUrl}`);

        //可以返回自定义转发内容，如果不返回，则系统执行默认分享行为。
        return {
            title: '超然视频',
            path: '/pages/video/video',
            imageUrl: '/static/images/nvsheng.jpg'
        }
    },

    handleSearch() {
        wx.navigateTo({url: '/pages/search/search'})
    }
});
